package com.byc.adbcommandline;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        boolean permission = ShellUtils.checkRootPermission();
        Log.e(TAG, "onCreate: " + permission);

        TextView tvUninstallApp = findViewById(R.id.tv_uninstall_app);
        TextView tvStartApp = findViewById(R.id.tv_start_app);
        TextView tvCloseApp = findViewById(R.id.tv_close_app);
        TextView tvGetAppMessage = findViewById(R.id.tv_get_app_message);
        TextView tvGetApp3 = findViewById(R.id.tv_get_app_3);
        TextView tvGetAppInstallLocation = findViewById(R.id.tv_get_app_install_location);
        TextView tvOpenWebUrl = findViewById(R.id.tv_open_web_url);
        TextView tvOpenSettings = findViewById(R.id.tv_open_settings);
        tvUninstallApp.setOnClickListener(this);
        tvStartApp.setOnClickListener(this);
        tvCloseApp.setOnClickListener(this);
        tvGetAppMessage.setOnClickListener(this);
        tvGetApp3.setOnClickListener(this);
        tvGetAppInstallLocation.setOnClickListener(this);
        tvOpenWebUrl.setOnClickListener(this);
        tvOpenSettings.setOnClickListener(this);

        TextView tvGetDeviceAndroidVersion = findViewById(R.id.tv_get_device_android_version);
        TextView tvGetDeviceModel = findViewById(R.id.tv_get_device_model);
        TextView tvGetDeviceAndroidId = findViewById(R.id.tv_get_device_android_id);
        TextView tvGetDeviceMac = findViewById(R.id.tv_get_device_mac);
        TextView tvGetDeviceDisplay = findViewById(R.id.tv_get_device_display);
        TextView tvGetDeviceDensity = findViewById(R.id.tv_get_device_density);
        tvGetDeviceModel.setOnClickListener(this);
        tvGetDeviceDisplay.setOnClickListener(this);
        tvGetDeviceAndroidId.setOnClickListener(this);
        tvGetDeviceMac.setOnClickListener(this);
        tvGetDeviceAndroidVersion.setOnClickListener(this);
        tvGetDeviceDensity.setOnClickListener(this);

        TextView tvDeleteFile = findViewById(R.id.tv_delete_file);
        TextView tvMoveFile = findViewById(R.id.tv_move_file);
        tvDeleteFile.setOnClickListener(this);
        tvMoveFile.setOnClickListener(this);

        TextView tvKeyHome = findViewById(R.id.tv_key_home);
        TextView tvKeyBack = findViewById(R.id.tv_key_back);
        TextView tvKeyMenu = findViewById(R.id.tv_key_menu);
        TextView tvKeyVoiceUp = findViewById(R.id.tv_key_voice_up);
        TextView tvKeyVoiceDown = findViewById(R.id.tv_key_voice_down);
        TextView tvKeyVoiceMuteOrNot = findViewById(R.id.tv_key_voice_mute_or_not);
        TextView tvKeyScreenLightUp = findViewById(R.id.tv_key_screen_light_up);
        TextView tvKeyScreenLightDown = findViewById(R.id.tv_key_screen_light_down);
        tvKeyHome.setOnClickListener(this);
        tvKeyBack.setOnClickListener(this);
        tvKeyMenu.setOnClickListener(this);
        tvKeyVoiceUp.setOnClickListener(this);
        tvKeyVoiceDown.setOnClickListener(this);
        tvKeyVoiceMuteOrNot.setOnClickListener(this);
        tvKeyScreenLightUp.setOnClickListener(this);
        tvKeyScreenLightDown.setOnClickListener(this);
    }

    @SuppressLint({"NonConstantResourceId", "SdCardPath"})
    @Override
    public void onClick(View view) {
        if (view != null) {
            String cmd;
            switch (view.getId()) {
                case R.id.tv_uninstall_app://卸载APP
                    cmd = ShellUtils.UNINSTALL_APP("com.tj.entranceguard");
                    break;
                case R.id.tv_start_app://按包名启动APP
                    cmd = ShellUtils.START_APP_BY_PACKAGE_NAME("com.yuezhi.bancard");
                    break;
                case R.id.tv_close_app://关闭APP
                    cmd = ShellUtils.STOP_APP("com.tj.entranceguard");
                    break;
                case R.id.tv_get_app_message://获取应用详细信息
                    cmd = ShellUtils.GET_APP_MESSAGE("com.yuezhi.bancard");
                    break;
                case R.id.tv_get_app_3://获取三方应用包名
                    cmd = ShellUtils.GET_APP_3;
                    break;
                case R.id.tv_get_app_install_location://获取应用安装位置
                    cmd = ShellUtils.GET_APP_INSTALL_LOCATION;
                    break;
                case R.id.tv_open_web_url://启动默认浏览器打开一个网页
                    cmd = ShellUtils.OPEN_WEB("https://www.baidu.com/?tn=85070231_3_hao_pg");
                    break;
                case R.id.tv_get_device_android_version://获取设备Android系统版本
                    cmd = ShellUtils.GET_DEVICE_ANDROID_VERSION;
                    break;
                case R.id.tv_get_device_model://获取设备型号
                    cmd = ShellUtils.GET_DEVICE_MODEL;
                    break;
                case R.id.tv_get_device_android_id://获取AndroidId
                    cmd = ShellUtils.GET_DEVICE_ANDROID_ID;
                    break;
                case R.id.tv_get_device_mac://获取设备Mac地址
                    cmd = ShellUtils.GET_DEVICE_MAC;
                    break;
                case R.id.tv_get_device_display://获取设备屏幕分辨率
                    cmd = ShellUtils.GET_DEVICE_DISPLAY;
                    break;
                case R.id.tv_get_device_density://获取屏幕像素密度
                    cmd = ShellUtils.GET_DEVICE_DENSITY;
                    break;
                case R.id.tv_open_settings://打开系统设置
                    cmd = ShellUtils.OPEN_SETTINGS;
                    break;
                case R.id.tv_move_file://移动文件到指定路径（当前文件路径为展示效果，实际使用需替换为真实路径）
                    cmd = ShellUtils.REMOVE_FILE("/sdcard/screen.png","/sdcard/aa/screen.png");
                    break;
                case R.id.tv_delete_file://删除文件
                    cmd = ShellUtils.DELETE_FILE("/sdcard/screen.png");
                    break;
                case R.id.tv_key_home://home键
                    cmd = ShellUtils.KEY_HOME;
                    break;
                case R.id.tv_key_back://返回键
                    cmd = ShellUtils.KEY_BACK;
                    break;
                case R.id.tv_key_menu://菜单键
                    cmd = ShellUtils.KEY_MENU;
                    break;
                case R.id.tv_key_voice_up://音量放大
                    cmd = ShellUtils.KEY_VOICE_UP;
                    break;
                case R.id.tv_key_voice_down://音量缩小
                    cmd = ShellUtils.KEY_VOICE_DOWN;
                    break;
                case R.id.tv_key_voice_mute_or_not://静音及解除静音
                    cmd = ShellUtils.KEY_VOICE_MUTE_OR_NOT;
                    break;
                case R.id.tv_key_screen_light_up://提高屏幕亮度
                    cmd = ShellUtils.KEY_SCREEN_LIGHT_UP;
                    break;
                case R.id.tv_key_screen_light_down://降低屏幕亮度
                    cmd = ShellUtils.KEY_SCREEN_LIGHT_DOWN;
                    break;
                default:
                    cmd = "";
                    break;
            }
            Log.e(TAG, "onClick: " + cmd);
            if (!TextUtils.isEmpty(cmd)) {
                new Thread(() -> {
                    ShellUtils.CommandResult commandResult = ShellUtils.execCommand(cmd, true);
                    if (commandResult.result == 0) {
                        Log.e(TAG, "操作成功： " + commandResult.successMsg);
                    } else {
                        Log.e(TAG, "操作失败： " + commandResult.errorMsg);
                    }
                }).start();
            } else {
                Log.e(TAG, "命令行为空~");
            }
        }
    }
}